package org.ReportDesigner.designerPDF;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.log4j.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: Kirito
 * Date: 25.02.13
 * Time: 20:25
 * To change this template use File | Settings | File Templates.
 */
public class HeaderFooter extends PdfPageEventHelper {
    private static final Logger log = Logger.getLogger(HeaderFooter.class);

    int pageNumber = 0;
    private PdfPTable header;


    public void onOpenDocument(PdfWriter pdfWriter, Document document){

    }

    public void onChapter(PdfWriter pdfWriter, Document document, float paragraphPosition, Paragraph paragraph){

    }

    public void onStartPage(PdfWriter pdfWriter, Document document){
        pageNumber++;
    }

    public void onEndPage(PdfWriter pdfWriter, Document document){
        Rectangle rec = pdfWriter.getPageSize();
        if (pageNumber > 1){
            header.setTotalWidth(420);
            header.writeSelectedRows(0, -1, 88, rec.getHeight()- 15, pdfWriter.getDirectContent());
        }
        ColumnText.showTextAligned(pdfWriter.getDirectContent(),Element.ALIGN_CENTER,
                new Phrase(String.format("страница %d",pageNumber),FontFactory.getFont("arial", "cp1251")),
                (rec.getLeft()+rec.getRight())/2, rec.getBottom() + 10 , 0);

    }

    public void onCloseDocument(PdfWriter pdfWriter, Document document){

    }


    public void setHeader(PdfPTable header) {
        this.header = header;
    }
}
